use super::*;
pub async fn get_bing_wallpaper(ctx: &Context) -> eyre::Result<PathBuf> {
    let directory = ctx.directory.clone();
    if !directory.exists() || !directory.is_dir() {
        fs::create_dir_all(&directory)?;
        trace!("created a directory at {}", directory.display());
    }

    assert!(
        directory.exists(),
        "path : {} is not exist !",
        directory.display()
    );
    assert!(
        directory.is_dir(),
        "path : {} is not a directory !",
        directory.display()
    );

    const URL: &str = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";

    let resp = reqwest::get(URL).await?;
    let text = resp.text().await?;
    let json: serde_json::Value = serde_json::from_str(&text)?;
    let json = json.as_object().unwrap();

    let image_url = json["images"][0]["url"].as_str().unwrap();
    let image_url = format!("{}{}", &ctx.domain, image_url);

    trace!("json parsed completed url : {}", &image_url);

    let resp = reqwest::get(image_url).await?;

    let image_path = ctx.current_image_path.clone();

    fs::write(&image_path, resp.bytes().await?).unwrap();

    trace!("download completed, saved at {}", image_path.display());

    Ok(image_path)
}
